<?

require_once  $_SERVER['DOCUMENT_ROOT']."/../_lib/base/common.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.hotels.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.upload.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.template.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.destinations.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.photos.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.tree.php";   
require_once SITE_ROOT_PATH."/_lib/classes/class.user_auth.php";

	// See if user is logged in 
	$user_auth = new user_auth();
	$user_auth->user_auth_cp();

	$template = new template();


// FUNCTION // Display resort search page
function display_home($values=''){

	$tree = new tree();
	$destinations = new destinations();

?>


<h2>Resorts</h2>


<?
if($GLOBALS['note']){
	echo display_message($GLOBALS['note']);
}
?>

<div class="border_box">
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="get" >

<input type="hidden" name="search" value="1" />

<table class="libra">
<tr>
<td width="200"><strong>Prefix</strong></td>
<td>
<select name="prefix">	
<option value=""></option>
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
<option value="d">d</option>
<option value="e">e</option>
<option value="f">f</option>
<option value="g">g</option>
<option value="h">h</option>
<option value="i">i</option>
<option value="j">j</option>
<option value="k">k</option>
<option value="l">l</option>
<option value="m">m</option>
<option value="n">n</option>    
<option value="o">o</option>
<option value="p">p</option>
<option value="q">q</option>
<option value="r">r</option>    
<option value="s">s</option>
<option value="t">t</option>
<option value="u">u</option>
<option value="v">v</option>
<option value="w">w</option>
<option value="x">x</option>
<option value="y">y</option>
<option value="z">z</option>
</select>

</td>
</tr>
<tr>
<td><strong>Name</strong></td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td><strong>Parent Destination</strong></td>
<td>
<select name="relation_id">	
<option value="">Destination</option>
<option value=""></option>

<?

function get_category_single_level($parent_node = 0){
	
	$tree = new tree();


	$params['parent_id'] = $parent_node;
	$params['type_id'] = '1';
	$values = $tree->get_branch($params);

	$GLOBALS['spacer'] += 1;
		
	if($values){

		foreach($values as $value){

?>       
<option value="<?= $value['tree_id'] ?>"><?

$i = 2;
while($i <= $GLOBALS['spacer']){
	echo "&nbsp;&nbsp;&nbsp;";
	$i++;
}

echo $value['name'];


?></option>
<?

		get_category_single_level($value['tree_id']);

		
		}
		
	}
	
	$GLOBALS['spacer'] -= 1;

}


get_category_single_level();



?>

</select>
</td>
</tr>
</table>


<input type="submit" name="submit" value="submit"/>

</form>

</div>	


<br />


<?

	if(isset($_GET['search'])){
		if(isset($_GET['prefix'])) $params['prefix'] = $_GET['prefix'];
		if(isset($_GET['name'])) $params['name'] = $_GET['name'];
		if(isset($_GET['relation_id'])) $params['relation_id'] = $_GET['relation_id'];
		$params['type_id'] = '2';
		$values = $destinations->get_destinations($params);
		unset($params);
	}
	
	if(!empty($values)){
	
?>



<table class="aqua">
  <tr bgcolor="#DDEEF7">
    <th width="200">Name</th>
    <th width="100">Tree ID</th>
    <th width="250">Parent Destination</th>
    <th width="100">No. of Hotels</th>
</tr>

<? 


foreach($values as $value){

 ?>
  <tr>
    <td><a href="resorts.php?action=view&tree_id=<?= $value['tree_id'] ?>"><?= $value['name'] ?></a></td>
    <td><?= $value['tree_id'] ?></td>
    <td><?

	
	$params['tree_id'] = $value['parent_id'];
	$destination_values = $tree->get_branch($params);
	$destination_values = $destination_values[0];
	unset($params);
	echo $destination_values['name']." (".$destination_values['tree_id'].")";
	
	
	?></td>
    <td><?
	
	// has Hotels as childdren
	$params['parent_id'] = $value['tree_id'];
	$params['type_id'] = '3';
	$has_hotel_children = count($tree->get_children($params));
	unset($params);
	echo $has_hotel_children;
	
	
	?></td>
</tr>
<? } ?>

</table>




<?
}


}
// END FUNCTION














// FUNCTION // Display form
function display_form($values=null, $form_errors=null){

	$destinations = new destinations();

?>

<h2>Resorts</h2>

<h3>Step 1</h3>



<div class="border_box">

<form method="post" action="<?= $_SERVER['PHP_SELF'] ?>"  >


<fieldset><legend>General</legend></fieldset>

    <table class="libra">
      <tr> 
        <th width="200"><strong>
          <label>Destination name</label> 
        &nbsp;</strong></th>
        <td><input name="name" type="text" value="<?= $values['name'] ?>" size="70"></td>
      </tr>
      <tr> 
        <th><strong>
          <label>Description</label>
        </strong></th>
        <td><textarea name="description" cols="70" rows="12"><?= $values['description'] ?></textarea></td>
      </tr>
</table>
      
      <br />

      
      
<fieldset><legend>Airports</legend></fieldset>

    <table class="libra">
      <tr> 
        <th width="200"><strong>
          <label>Airport codes related list</label>
        </strong></th>
        <td><input name="airport_codes_related_list" type="text" value="<?= $values['airport_codes_related_list'] ?>" size="50" /><img src="/images/icon_question.gif" class="icon_qm"  title="SABS Airport Code" alt="This is ..." width="11" height="19" /></td>
      </tr>
</table>


<br />

      
      
<fieldset><legend>Misc</legend></fieldset>

    <table class="libra">
      <tr> 
        <th width="200"><strong>
          <label>Map image url </label>
        </strong></th>
        <td><input name="map_image_url" type="text" value="<?= $values['map_image_url'] ?>" size="70"></td>
      </tr>
      <tr> 
        <th><strong>
          <label>Yahoo weather location id</label>
        </strong></th>
        <td><input name="yahoo_weather_location_id" type="text" value="<?= $values['yahoo_weather_location_id'] ?>" size="15"></td>
      </tr>
      <tr> 
        <th><strong>
          <label>YouTravel ID</label>
        </strong></th>
        <td><input name="youtravel_id" type="text" value="<?= $values['youtravel_id'] ?>" size="15"> <a href="http://testxml.youtravel.com/webservicestest/get_destinations.asp?LangID=EN&amp;Username=xmltestme&amp;Password=testme">XML list of resorts</a></td>
      </tr>
</table>

<br />

      
<fieldset><legend>Longtitude & Latitude</legend></fieldset>

    <table class="libra">
    
      <tr> 
        <th width="200"><strong>
          <label>Longtitude</label>
        </strong></th>
        <td><input name="longtitude" type="text" value="<?= $values['longtitude'] ?>" /></td>
      </tr>
      <tr> 
        <th><strong>
          <label>Latitude</label>
        </strong></th>
        <td><input name="latitude" type="text" value="<?= $values['latitude'] ?>" /></td>
      </tr>
    
      <tr> 
        <th><strong>
          <label>Google Maps</label>
        </strong></th>
        <td><input type="checkbox" name="show_google_map" value="1" <?= checked($values['show_google_map'], '1') ?> /></td>
      </tr>
    </table>


	<input type="hidden" name="action" value="step_2" />
	<input type="hidden" name="tree_id" value="<?= $values['tree_id'] ?>" />
	<input type="hidden" name="parent_node" value="<?= $_REQUEST['parent_node'] ?>" />
	
	<div class="a_center">
	<input type="button" value="Cancel"  onclick="parent.location='<?= $_SERVER['PHP_SELF'] ?>'" class="form_button" />
	<input type="submit" value="Submit" name="submit" class="form_button" />
	</div>

</form>

</div>

<?



}
// END FUNCTION 






// FUNCTION // Display photos form 
function display_photos_form($form_errors=''){


?>

<h2>Resorts</h2>

<h3>Step 2</h3>



<div class="border_box">





<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data"  >

<input type="hidden" name="action" value="upload_photo" />
<input type="hidden" name="form_submitted" value="true" />


<?
if(!empty($_SESSION['destination']['photos'])){
	$uploaded_count = count($_SESSION['destination']['photos']);
}else{
	$uploaded_count = 0;
}


if($uploaded_count < '6'){
?>

<fieldset><legend>Upload new photo</legend></fieldset>

<?
if($form_errors){
	$my_message = array();
	$my_message['type'] = 'neg';
	$my_message['message'] = "There was a problem with the information submitted";
	$my_message['list'] = $form_errors;
	echo display_message($my_message);
}
?>


<?

	if($uploaded_count){
		$i = $uploaded_count;
	}else{
		$i = 0;
	}


?>
<table class="libra">
<tr>
<td width="150"><label>Photo <?= $i+1 ?></label></td>
<td><input name="photo_<?= $i ?>" type="file" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="submit" /></td>
</tr>
</table>

</form>

<? } ?>



<?
if(!empty($uploaded_count)){
?>


<fieldset><legend>Current photos</legend></fieldset>



<table class="libra">
<?
for($j=0;$j<$uploaded_count;$j++){
?>

<tr>
<td width="150">Photo <?= $j+1 ?></td>
<td width="120"><img src="<?= UPLOAD_HTTP_TEMP.$_SESSION['destination']['photos'][$j]['photo_url_thumb'] ?>" /></td>
<td>

<? if($j==0) { ?>
<h5>Main photo</h5>
<ul class="qlinks"><li><a href="<?= $_SERVER['PHP_SELF'] ?>?action=delete_photo&photo_no=<?= $j ?>" onclick="return confirm('Do you really want to delete this photo?')">Delete this photo</a></li></ul>
<? }else{ ?>
<ul class="qlinks">
<li><a href="<?= $_SERVER['PHP_SELF'] ?>?action=make_main&photo_no=<?= $j ?>">Make this the main photo</a></li>
<li><a href="<?= $_SERVER['PHP_SELF'] ?>?action=delete_photo&photo_no=<?= $j ?>" onclick="return confirm('Do you really want to delete this photo?')">Delete this photo</a></li>
</ul>
<? } ?>


</td>
</tr>

<? } ?>
</table>
<? } ?>




<form action="<?= $_SERVER['PHP_SELF'] ?>" method="get">

    <input type="hidden" name="action" value="step_3"/>
 
    <input type="button" value="Back" onclick="history.back();" />
    <input type="submit" name="submit" value="Next" />

</form>




<?



}
// END FUNCTION 




// FUNCTION // View individual destination 
function preview_advert($values){

	$destinations = new destinations();



?>


<h2>Resorts</h2>

<h3>Step 3</h3>


<div class="border_box">


<fieldset><legend>General</legend></fieldset>



<table class="libra">
	<tr>
		<td width="200"><strong>Destination</strong></td>
		<td><?= $values['name'] ?></td>
	</tr>
	<tr>
		<td><strong>Description</strong></td>
		<td><?= nl2br($values['description']) ?></td>
	</tr>
    </table>
    
   <br />
 
<fieldset><legend>Airports</legend></fieldset>



<table class="libra">
	<tr>
		<td width="200"><strong>Airport codes related list</strong></td>
		<td><?= $values['airport_codes_related_list'] ?></td>
	</tr>

    </table>
    <br />

    
<fieldset><legend>Misc</legend></fieldset>



<table class="libra">
	<tr>
		<td width="200"><strong>Map image url </strong></td>
		<td><?
		
		
if(!$values['map_image_url']){
	echo "na";
}else{
?>
	
	<img src="<?= UPLOAD_HTTP_PERM.$values['map_image_url']; ?>" />
<?
} 
		
		
		
		?></td>
	</tr>
	<tr>
		<td><strong>Yahoo weather location id</strong></td>
		<td><?= $values['yahoo_weather_location_id'] ?></td>
	</tr>
	<tr>
		<td><strong>YouTravel ID</strong></td>
		<td><?= $values['youtravel_id'] ?></td>
	</tr>
</table>

<br />



<fieldset><legend>Longtitude & Latitude</legend></fieldset>



<table class="libra">
	<tr>
		<td width="200"><strong>Longtitude</strong></td>
		<td><?= $values['longtitude'] ?></td>
	</tr>
	<tr>
		<td><strong>Latitude</strong></td>
		<td><?= $values['latitude'] ?></td>
	</tr>
	<tr>
		<td><strong>Show Google Map</strong></td>
		<td><?  
		
if($values['show_google_map']=='1'){
	echo "yes";
}else{
	echo "no";
}
		
?></td>
	</tr>
</table>

<br />
        

<fieldset><legend>Photos</legend></fieldset>



<table class="libra">
	<tr>
		<td width="200"><strong>Photos</strong></td>
		<td>


<?




	for($i = 0; $i < count($_SESSION['destination']['photos']); $i++){
		
		?><img src='<?= UPLOAD_HTTP_TEMP.$_SESSION['destination']['photos'][$i]['photo_url_thumb'] ?>' class='thumbnail'><?
			
	}
    
?>

</td>
	</tr>
</table>


<br class="clearing"/>
<br />
<br />



<form action="<?= $_SERVER['PHP_SELF'] ?>">

    <input type="hidden" name="action" value="step_4">

	<center>
    <input type="button" value="Back" onclick="history.back();" class="form_button">
	<input type="submit" value="Save" class="form_button" />
	</center>

</form>


</div>
<?


}
// END FUNCTION





// FUNCTION // View individual destination
function view_details($values){

	$destinations = new destinations();



?>


<h2>Resorts</h2>

<h3>View resort details</h3>

<?
if($GLOBALS['note']){
	echo display_message($GLOBALS['note']);
}
?>

<div class="border_box">


<ul>
<li><a href="resorts.php?action=step_1_edit&tree_id=<?= $values['tree_id'] ?>">Edit details</a></li>
<li><a href="resorts.php?action=delete&tree_id=<?= $values['tree_id'] ?>" onclick="return confirm('Do you really want to delete this resort?')"  >Delete this resort</a></li>
</ul>


<fieldset><legend>General</legend></fieldset>



<table class="libra">
	<tr>
		<td width="200"><strong>Resort name</strong></td>
		<td><?= $values['name'] ?></td>
	</tr>
	<tr>
		<td><strong>Tree ID</strong></td>
		<td><?= $values['tree_id'] ?></td>
	</tr>
	<tr>
		<td><strong>Description</strong></td>
		<td><?= nl2br($values['description']) ?></td>
	</tr>
    </table>
    
   <br />
 
<fieldset><legend>Airports</legend></fieldset>



<table class="libra">
	<tr>
		<td width="200"><strong>Airport codes related list</strong></td>
		<td><?= $values['airport_codes_related_list'] ?></td>
	</tr>
</table>


<br />

    
<fieldset><legend>Misc</legend></fieldset>



<table class="libra">
	<tr>
		<td width="200"><strong>Map image url </strong></td>
		<td><?
		
		
if(!$values['map_image_url']){
	echo "na";
}else{
?>
	
	<img src="<?= UPLOAD_HTTP_PERM.$values['map_image_url']; ?>" />
<?
} 
		
		
		
		?></td>
	</tr>
	<tr>
		<td><strong>Yahoo weather location id</strong></td>
		<td><?= $values['yahoo_weather_location_id'] ?></td>
	</tr>
	<tr>
		<td><strong>YouTravel ID</strong></td>
		<td><?= $values['youtravel_id'] ?></td>
	</tr>
</table>

<br />



<fieldset><legend>Longtitude & Latitude</legend></fieldset>



<table class="libra">
	<tr>
		<td width="200"><strong>Longtitude</strong></td>
		<td><?= $values['longtitude'] ?></td>
	</tr>
	<tr>
		<td><strong>Latitude</strong></td>
		<td><?= $values['latitude'] ?></td>
	</tr>
	<tr>
		<td><strong>Show Google Map</strong></td>
		<td><?  
		
if($values['show_google_map']=='1'){
	echo "yes";
}else{
	echo "no";
}
		
?></td>
	</tr>
</table>

<br />
        

<fieldset><legend>Photos</legend></fieldset>



<table class="libra">
	<tr>
		<td width="200"><strong>Photos</strong></td>
		<td>


<?
	$photos = new photos;
    $current_photos = $photos->get_photos($values['tree_id']);

    if(!$current_photos){
		echo "<img src='".UPLOAD_HTTP_PERM."na.gif'>";
	}else{

		foreach($current_photos as $current_photo){
			
			echo "<img src='".UPLOAD_HTTP_PERM.$current_photo['photo_url_thumb']."' class='thumbnail' >";

		}

    }
    
?>

</td>
	</tr>
</table>


<br />
        

<fieldset><legend>Parent destination</legend></fieldset>



<table class="libra">
	<tr>
		<td width="200"><strong>Parent destination</strong></td>
		<td>

<?
	$tree = new tree();	
	$params['tree_id'] = $values['parent_id'];
	$destination_values = $tree->get_branch($params);
	$destination_values = $destination_values[0];
	unset($params);
	


?>
<a href="destinations.php?action=view&tree_id=<?= $destination_values['tree_id'] ?>"><?= $destination_values['name']." (".$destination_values['tree_id'].")"; ?></a>
</td>
	</tr>
</table>


<br />

  <?
  
  	$hotels = new hotels();
	$params['parent_id'] = $values['tree_id'];
	$child_hotels = $tree->get_branch($params,3);

	unset($params);
	if($child_hotels){
		
	?>

<fieldset><legend>Hotels</legend></fieldset>



<table class="libra">
	<tr>
		<td width="200"><strong>Hotels</strong></td>
		<td>
<ul>
<?



		foreach($child_hotels as $hotel_values){
			echo "<li><a href=\"hotels.php?action=view&tree_id=".$hotel_values['tree_id']."\" >".$hotel_values['name']."</a></li>";
		}
?>
</ul>
</td>
	</tr>
</table>


<br />

<?	}


?>



</div>
<?


}
// END FUNCTION 








switch ($_REQUEST['action']) {
    default; 
		$template->head_control();
		display_home();
		$template->foot_control();
    break;
	case "step_1_insert";
		if(!isset($_GET['parent_tree_id'])){
			header("Location: resorts.php");
			exit;
		}else{
			// clear any old advert session
			unset($_SESSION['destination']);
		
			$tree = new tree();
			$params['tree_id'] = $_GET['parent_tree_id'];
			$values_parent = $tree->get_branch($params,1);
			$values_parent = $values_parent[0];
			$_SESSION['destination']['parent_id'] = $values_parent['tree_id'];
			$_SESSION['destination']['lineage_base'] = $values_parent['lineage'];
			$_SESSION['destination']['type'] = "new";
			$_SESSION['destination']['type_id'] = '2';
			
			$template->head_control();
			display_form();
			$template->foot_control();
		}
    break;
	case "step_1_edit";
		// clear any old advert session
       
		if(isset($_GET['tree_id'])){
		 
		 	unset($_SESSION['destination']);
			
			$tree = new tree();
			$params['tree_id'] = $_GET['tree_id'];
			$values_destination = $tree->get_branch($params,1);
			$_SESSION['destination'] = $values_destination[0];
			$_SESSION['destination']['type'] = "edit";
			
			// Photos
			$photos = new photos();
			$current_photos = $photos->get_photos($values_destination[0]['tree_id']);
			if(!empty($current_photos)){
				foreach($current_photos as $photo_values){
					// copy the exsiting images to the temp diretory
					@copy(UPLOAD_ABS_PERM.$photo_values['photo_url_full'], UPLOAD_ABS_TEMP.$photo_values['photo_url_full']);
					@copy(UPLOAD_ABS_PERM.$photo_values['photo_url_thumb'], UPLOAD_ABS_TEMP.$photo_values['photo_url_thumb']);
				}
				$_SESSION['destination']['photos'] = $current_photos;
			}
			
			$template->head_control();
			display_form($_SESSION['destination']);
			$template->foot_control();	
					
		}else{
			header("Location: resorts.php");
			exit;
		}

    break;
    case "step_2";
	
		$template->head_control();
		
		$_SESSION['destination']['name'] = $_REQUEST['name']; 
		$_SESSION['destination']['description'] = $_REQUEST['description'];
		$_SESSION['destination']['airport_codes_related_list'] = $_REQUEST['airport_codes_related_list'];
		$_SESSION['destination']['youtravel_id'] = $_REQUEST['youtravel_id'];
		$_SESSION['destination']['map_image_url'] = $_REQUEST['map_image_url'];
		$_SESSION['destination']['yahoo_weather_location_id'] = $_REQUEST['yahoo_weather_location_id'];
		$_SESSION['destination']['longtitude'] = $_REQUEST['longtitude'];
		$_SESSION['destination']['latitude'] = $_REQUEST['latitude'];
		$_SESSION['destination']['show_google_map'] = $_REQUEST['show_google_map'];
		
		
        $destination = new destinations();
        $errors_found = $destination->validate_details_form($_SESSION['destination']);
		
        if($errors_found) {
            display_form($_SESSION['destination'],$errors_found);
        }else{
            display_photos_form();
        }
		$template->foot_control();
    break;
	
	
   case "upload_photo";
	

	
		if(!isset($_SESSION['destination'])){
			// redirect browser first step if there is no advert session data
			header("Location: destination.php");
			exit;
		}
	
		// Check to see if the form was submitted
		// If yes then set the values
		if(isset($_POST['form_submitted'])){

        for($i=0; $i<6; $i++){
		
            if($_FILES['photo_'.$i]['size']){
				
                $my_upload = new uploader();
                $upload = $my_upload->upload_file('photo_'.$i, MAX_FILE_SIZE);
				
                if($upload['error']){
                    
					$my_array_errors = array($upload['error']);
					
                }else{
					
					// create a unique name
                    $unique_file_name_img = unique_id();
					// Create a full size image from uploaded image
                    $my_upload->optimise_photo_image(FULL_HEIGHT, FULL_WIDTH, $upload['source'], UPLOAD_ABS_TEMP, $unique_file_name_img, $upload['ext']);
					
					// create a unique name
					$unique_file_name_img_thumb = unique_id(); 
					// Create a smaller size image from uploaded image
                    $my_upload->optimise_photo_image(THUMB_HEIGHT, THUMB_WIDTH, $upload['source'], UPLOAD_ABS_TEMP, $unique_file_name_img_thumb, $upload['ext']);
					
					// add the photo urls to the advert session
					$_SESSION['destination']['photos'][$i]['photo_url_full'] = $unique_file_name_img.".jpg"; 
					$_SESSION['destination']['photos'][$i]['photo_url_thumb'] = $unique_file_name_img_thumb.".jpg"; 
	
					
                }
            }
        }
		
			// redirect browser to search results page
			// avoid form being resubmitted repeat when refreshing browser
			header("Location: resorts.php?action=upload_photo");
			exit;
		
		
		}else{
		
			$template->head_control();
	
			display_photos_form();
	
			$template->foot_control();	
		
		}
		
		
		
    break;
    case "delete_photo";
	
		if(!isset($_SESSION['destination'])){
			// redirect browser first step if there is no advert session data
			header("Location: /destination.php");
			exit;
		}
		
		$delete_no = $_REQUEST['photo_no']; 

		unset($_SESSION['destination']['photos'][$delete_no]);
		
		$new_advert_photos_array = array();
		
		foreach($_SESSION['destination']['photos'] as $values){
			$new_advert_photos_array[] = $values;
		}
		
		// Delete session values
		unset($_SESSION['destination']['photos']);
		// Reasign values
		$_SESSION['destination']['photos'] = $new_advert_photos_array;
		
		$template->head_control();

		display_photos_form();

		$template->foot_control();
		
    break;
	case "make_main":
	
		if(!isset($_SESSION['destination'])){
			// redirect browser first step if there is no advert session data
			header("Location: /ads/");
			exit;
		}


		$c_photos = $_SESSION['destination']['photos'];
		$n_photos = array();
		$main_photo_no = (int)$_GET['photo_no'];
		
		$n_photos[] = $c_photos[$main_photo_no];
		
		$i = 0;
		

		foreach($c_photos as $c_photo){
			
			if($i !== $main_photo_no){
				$n_photos[] = $c_photo;
			}
			
			$i++;
		}
		
		$_SESSION['destination']['photos'] = $n_photos;
		
		$template->head_control();

		display_photos_form();

		$template->foot_control();

	
	break;
	
	
	
	
    case "step_3";

		
		$template->head_control();
        preview_advert($_SESSION['destination']);
		$template->foot_control();
		
    break;

    
    case "step_4"; // COMIT DETAILS
		
		$tree = new tree();
		$destinations = new destinations();
        $my_upload = new uploader();
        $photos = new photos();
		
		// get main_photo value
		$_SESSION['destination']['main_photo'] = $_SESSION['destination']['photos'][0]['photo_url_thumb'];
	
		// Insert details
		if($_SESSION['destination']['type'] == "new"){

			//Get lineage from resort id
			$params['tree_id'] = $_SESSION['destination']['parent_id'];
			$parent_branch = $tree->get_branch($params,1);
			$_SESSION['destination']['lineage_base'] = $parent_branch[0]['lineage'];
			$_SESSION['destination']['type_id'] = '2';
			$tree_id = $tree->insert_branch($_SESSION['destination'],2);
		}
		
		// update details
		if($_SESSION['destination']['type'] == "edit"){
			
			$tree_id = $_SESSION['destination']['tree_id'];
			$tree->update_branch($_SESSION['destination'],2); 
			if(!empty($_SESSION['destination']['photos'])){
				// Delete photo files
				$all_exsiting_photos = $photos->get_photos($tree_id);
				if(!empty($all_exsiting_photos)){
					foreach($all_exsiting_photos as $exsiting_photo){
						@unlink(UPLOAD_ABS_PERM.$exsiting_photo['photo_url_thumb']);
						@unlink(UPLOAD_ABS_PERM.$exsiting_photo['photo_url_full']);
					}		
					// Delete previous photos entries 
					$photos->delete_photos($_SESSION['destination']['tree_id']);
				}
			}
		}

		
		$photos->upload_form_photo($_SESSION['destination']['photos'], $tree_id);
		

		// clear any old advert session
		unset($_SESSION['destination']); 
		header("Location: resorts.php?action=updated&tree_id=".$tree_id);
		
    break;  
	
	case "uploaded";
		$my_message = array();
		$my_message['type'] = 'pos';
		$my_message['message'] = "Resort succesfully updated";
		$GLOBALS['note'] = $my_message;
    
		$template->head_control();
		display_home();
		$template->foot_control();
    break; 
	 
	
    case "photo_delete";
		$template->head_control();
        $tree = new tree();
        $dtree->delete_photo($_REQUEST['tree_id'],$_REQUEST['photo_id']);
        display_photos_form($_REQUEST['tree_id'],'');
		$template->foot_control();
    break;  
	
	
	case "view";
		$template->head_control();
        
		if(isset($_GET['tree_id'])){
			$tree = new tree();
			$params['tree_id'] = $_GET['tree_id'];
			$resort = $tree->get_branch($params);
			view_details($resort[0]);
		}
		
		$template->foot_control();
	break;
	
	
	case "updated";
	
		$my_message = array();
		$my_message['type'] = 'pos';
		$my_message['message'] = "Destination succesfully updated";
		$GLOBALS['note'] = $my_message; 
	
		$template->head_control();
        
		if(isset($_GET['tree_id'])){
			$tree = new tree();
			$params['tree_id'] = $_GET['tree_id'];
			$resort = $tree->get_branch($params,1);
			view_details($resort[0]);
		}
		
		$template->foot_control();
	break;
	

	case "delete";
	
		// Delete destination
		$destinations = new destinations();
		$destinations->delete_destination($_REQUEST['tree_id']);
		$tree = new tree();
		// Delete branch
		$params['tree_id'] = $_REQUEST['tree_id'];
		$tree->delete_branch($params);
		unset($params);
		$photos = new photos();
		// Delete photos
		$photos->delete_photos($_REQUEST['tree_id']);

		
		$my_message = array();
		$my_message['type'] = 'pos';
		$my_message['message'] = "Resort deleted succesfully.";
		$GLOBALS['note'] = $my_message;

		$template->head_control();
		display_home();
		$template->foot_control();
	break;
}


?>